.DEFAULT_GOAL := help
# 以目录名作为镜像，应用文件名
CURDIRNAME?=$(shell basename ${CURDIR})
# 可用参数定义 CURDATE 的值
CURDATE?=$(shell date '+%Y%m%d')
REGISTRY=registry.cn-shanghai.aliyuncs.com/haifengat
AppName=${CURDIRNAME}
$(shell mkdir -p ./lib)
$(shell \cp ~/go/pkg/mod/gitee.com/haifengat/goctp/v2\@$(shell cat go.mod |grep goctp|sed 's:\s\+:\t:g'|cut -f3)/lib/*.so ./lib)
#================================= 镜像处理 =============================#
build: # 编译 打包镜像
	go build -o ${CURDIRNAME} cmd/main.go
	@# 替换容器中的 appName 以解决 entrypoint 启动程序不能用变量的问题
	sed -i 's#ENTRYPOINT.*#ENTRYPOINT ["./${CURDIRNAME}"]#g' Dockerfile
	docker build . -t ${REGISTRY}/${CURDIRNAME}:${CURDATE} --no-cache --build-arg AppName=${CURDIRNAME}
	sed -i 's#ENTRYPOINT.*#ENTRYPOINT ["./$${AppName}"]#g' Dockerfile
	rm -rf ./lib
	rm -f ${CURDIRNAME}
docker: build # 镜像推送
	docker push ${REGISTRY}/${CURDIRNAME}:${CURDATE}
# 镜像发送 latest
	docker tag ${REGISTRY}/${CURDIRNAME}:${CURDATE} ${REGISTRY}/${CURDIRNAME}:latest
	docker push ${REGISTRY}/${CURDIRNAME}:latest
#=================================== 部署 =====================================#
.PHONY: build docker
.PHONY: help
help:
	@echo '镜像生成并推送:     make docker'
	@echo 'make -n 检查语法'
	@echo 'make xxxx CURDATE=yyyymmdd 指定日期(版本)'
